package com.maochd.cloud.excel.service.impl;

import com.maochd.cloud.api.goods.domain.condition.GoodsQueryCondition;
import com.maochd.cloud.api.goods.domain.entity.GoodsInfo;
import com.maochd.cloud.api.goods.service.RemoteGoodsService;
import com.maochd.cloud.common.core.domain.R;
import com.maochd.cloud.common.core.utils.bean.BeanConvertUtil;
import com.maochd.cloud.excel.annotation.ExportMethod;
import com.maochd.cloud.excel.dto.ExportGoodsDTO;
import com.maochd.cloud.excel.dto.excel.ExportGoodsExcel;
import com.maochd.cloud.excel.enums.ExportEnum;
import com.maochd.cloud.excel.utils.SpringUtils;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ExportBusinessServiceImpl {

    @Resource
    private RemoteGoodsService remoteGoodsService;

    @ExportMethod(type = ExportEnum.EE_1001)
    @SneakyThrows
    public List<List<?>> exportGoods(ExportGoodsDTO dto) {
        GoodsQueryCondition condition = BeanConvertUtil.convertTo(dto, GoodsQueryCondition::new);
        R<List<GoodsInfo>> result = remoteGoodsService.list(condition);
        if (R.isNotOk(result)) {
            return SpringUtils.exportConvert(SpringUtils.generateListWithEmpty(ExportGoodsExcel.class));
        }
        return SpringUtils.exportConvert(BeanConvertUtil.convertListTo(result.getData(), ExportGoodsExcel::new));
    }
}
